package com.kyxx.topics.net;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.kyxx.topics.R;
import com.kyxx.topics.utils.Log;
import com.kyxx.topics.utils.TextUtil;
import com.kyxx.topics.utils.Tools;
import com.kyxx.topics.widget.LoadingDialog;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import cz.msebera.android.httpclient.Header;

public abstract class ResponseListener extends AsyncHttpResponseHandler {

    /**
     * 如果需要用到Dialog.context请传Activity
     */
    private Context context;

    private boolean hasDialog = true;

    private LoadingDialog dialog;
    private LinearLayout error;
    private LinearLayout loading;

    /**
     * 默认只有dialog加载样式
     *
     * @param context
     * @param
     */
    public ResponseListener(Context context) {
        this(context, true);
    }

    /**
     * 无dialog加载
     *
     * @param context
     * @param noDialog
     */
    public ResponseListener(Context context, boolean noDialog) {
        this.context = context;
        this.hasDialog = noDialog;
    }

    /**
     * 无dialog加载,有界面加载样式
     *
     * @param context
     * @param
     */
    public ResponseListener(Context context, LinearLayout loading,
                            LinearLayout error) {
        this.hasDialog = false;
        this.context = context;
        this.loading = loading;
        this.error = error;
    }

    @Override
    public void onStart() {
        if (hasDialog) {
            dialog = new LoadingDialog(context, R.style.Loading_Dialog);
            dialog.show();
        }
        if (loading != null) {
            loading.setVisibility(View.VISIBLE);
        }
        if (error != null) {
            error.setVisibility(View.GONE);
        }
    }

    @Override
    public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
        String message = "网络不给力";
        if (arg2 != null && arg2.length > 0) {
            message = new String(arg2);
        }
        if (error != null) {
            error.setVisibility(View.VISIBLE);
        }
//        failed(arg0, arg1, new Exception(message, arg3));
    }


    @Override
    public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {

        try {
            String result = new String(arg2, "utf-8");
            Log.e("result", result);
            JSONObject object = new JSONObject(result);
            int code = object.getInt("code");
            if (0 == code) {
                success(arg0, arg1, object);
            } else {
                if (!TextUtils.isEmpty(object.getString("msg")))
                    Tools.toast(context, object.getString("msg"));
//				msg = StringUtil.getError(msg, code, context, object);
//				if (code == 100) {
//					PreferenceUtil.putString("token", "");
//				}else
//				{
                failed(arg0, arg1, object);
//				}
            }
        } catch (UnsupportedEncodingException e) {
//            failed(arg0, arg1, e);
        } catch (JSONException e) {
//            failed(arg0, arg1, e);
        }

    }

    public abstract void success(int arg0, Header[] arg1, JSONObject result);

    public void failed(int arg0, Header[] arg1, JSONObject result) {
//        Log.e("error", e.getMessage());
//        if (!TextUtils.isEmpty(e.getMessage())) {
//            ToastUtils.showToast(context, e.getMessage());
//        }
    }

    @Override
    public void onFinish() {
        if (dialog != null) {
            dialog.dismiss();
        }
        if (loading != null) {
            loading.setVisibility(View.GONE);
        }
        super.onFinish();
    }
}
